Skip to content

Fix copying fp64 host arrays to a device without fp64 in from_dlpack - #3209

Merged
antonwolfy merged 3 commits into
masterfrom
dlpack-host-copy-no-fp64
Nov 30, 2026
Merged

antonwolfy merged 3 commits into
masterfrom
dlpack-host-copy-no-fp64

Conversation

@antonwolfy

@antonwolfy antonwolfy commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

This PR fixes wrong values from dpnp.from_dlpack and dpnp.tensor.from_dlpack when copying a float64 or complex128 host array to a SYCL device without fp64 support.

When the producer cannot export to the requested oneAPI device, as NumPy cannot, from_dlpack imports the array on the host and copies it to the device with _to_usm_ary_from_host_blob. On a device without fp64, that helper makes the result float32 or complex64, but it copied the bytes of the float64 or complex128 host array into it unchanged, with no error. For example, [1.5, -2.25, 3.0] came out as [0, 1.9375, 0]. The host array is now cast to the result's data type before the copy, as dpnp.tensor.asarray does for host arrays.

test_copy_via_host_fp64 copies float64 and complex128 host arrays to the device. It expects the same data type on a device with fp64 and float32 or complex64 on one without, and compares the values in both cases. The CI devices all support fp64, so there it only checks the fp64 case.

  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to an issue with a reproducer?
  • Have you tested your changes locally for CPU and GPU devices?
  • Have you made sure that new changes do not introduce compiler warnings?
  • Have you checked performance impact of proposed changes?
  • Have you added documentation for your changes, if necessary?
  • Have you added your changes to the changelog?

… `from_dlpack`

On a device without fp64 support, `_to_usm_ary_from_host_blob` made the result `float32` or `complex64` for a `float64` or `complex128` host array, but copied the host bytes into it unchanged, so `dpnp.from_dlpack` and `dpnp.tensor.from_dlpack` returned wrong values. The host array is now cast to the result's data type before the copy, as `dpnp.tensor.asarray` does.

`test_copy_via_host_fp64` copies `float64` and `complex128` host arrays to the device and checks the data type and values of the result.
@antonwolfy antonwolfy added this to the 0.22.0 release milestone Nov 30, 2026
@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

View rendered docs @ https://intelpython.github.io/dpnp/index.html

@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Array API standard conformance tests for dpnp=0.22.0dev3=np2py314h8d9cdd5_67 ran successfully.
Passed: 1491
Failed: 0
Skipped: 6

@coveralls

coveralls commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 84.557% (-0.002%) from 84.559% — dlpack-host-copy-no-fp64 into master

@antonwolfy

Copy link
Copy Markdown
Contributor Author

Tested on Intel Iris Xe Graphics, which has no fp64 support (Level Zero, compute runtime 26.40.40196.15), and on a 12th Gen Intel Core i7 CPU (OpenCL, with fp64), with DPC++ 2026.2 and NumPy 2.5.5:

  • test_copy_via_host_fp64, dpnp/tests/tensor/test_usm_ndarray_dlpack.py and dpnp/tests/test_dlpack.py pass on both devices.
  • With the fix reverted, test_copy_via_host_fp64 fails both cases on the GPU ([1.5, -2.25, 3.0] comes out as [0, 1.9375, 0]) and still passes on the CPU.
  • On the GPU, dpnp.from_dlpack and dpnp.tensor.from_dlpack of float64 and complex128 host arrays now match dpnp.tensor.asarray bit for bit, including subnormal, NaN, inf, -0.0 and values that overflow float32 (with the same RuntimeWarning).

Host arrays whose last axis is not contiguous still raise ValueError; #3207 fixes that.

@ndgrigorian ndgrigorian left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@antonwolfy
antonwolfy merged commit 1c7974c into master Nov 30, 2026
44 of 45 checks passed
@antonwolfy
antonwolfy deleted the dlpack-host-copy-no-fp64 branch November 30, 2026 18:47
github-actions Bot added a commit that referenced this pull request Nov 30, 2026
#3209)

This PR fixes wrong values from `dpnp.from_dlpack` and
`dpnp.tensor.from_dlpack` when copying a `float64` or `complex128` host
array to a SYCL device without fp64 support.

When the producer cannot export to the requested oneAPI device, as NumPy
cannot, `from_dlpack` imports the array on the host and copies it to the
device with `_to_usm_ary_from_host_blob`. On a device without fp64, that
helper makes the result `float32` or `complex64`, but it copied the
bytes of the `float64` or `complex128` host array into it unchanged,
with no error. For example, `[1.5, -2.25, 3.0]` came out as `[0, 1.9375,
0]`. The host array is now cast to the result's data type before the
copy, as `dpnp.tensor.asarray` does for host arrays.

`test_copy_via_host_fp64` copies `float64` and `complex128` host arrays
to the device. It expects the same data type on a device with fp64 and
`float32` or `complex64` on one without, and compares the values in both
cases. The CI devices all support fp64, so there it only checks the fp64
case.

- [x] Have you provided a meaningful PR description?
- [x] Have you added a test, reproducer or referred to an issue with a
reproducer?
- [x] Have you tested your changes locally for CPU and GPU devices?
- [x] Have you made sure that new changes do not introduce compiler
warnings?
- [ ] Have you checked performance impact of proposed changes?
- [ ] Have you added documentation for your changes, if necessary?
- [x] Have you added your changes to the changelog?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants